<template>
  <!-- 后台用户页面 -->
  <div>
    <el-card>
    <!-- 模糊查框 添加按钮 -->
    <div class="abvoeBox">
        用户名称:<el-input placeholder="请输入用户名称" v-model="userName" class="abvoeBox_input"></el-input>
        <el-button type="primary" @click="selectUserProtalEntity()" style="margin-left:10px">查询</el-button>
    </div>
    <!-- 全查数据 -->
    <div class="belowBox">
       <el-table
            :data="tableData"
            border
            style="width: 100%">
            <el-table-column
            fixed
            prop="id"
            label="序号">
            </el-table-column>
            <el-table-column
            prop="userName"
            label="用户名称">
            </el-table-column>
            <el-table-column
            prop="nickName"
            label="用户昵称">
            </el-table-column>
            <el-table-column
            prop="sex"
            label="性别">
            <template slot-scope="scope">
                <p>{{scope.row.sex===0?"男":"女"}}</p>
            </template>
            </el-table-column>
            <el-table-column
            prop="age"
            label="用户年龄">
            </el-table-column>
            <el-table-column
            prop="phone"
            label="用户电话">
            </el-table-column>
            <el-table-column
            prop="mail"
            label="用户邮箱">
            </el-table-column>
            <el-table-column
            prop="birthday"
            label="出生日期">
            </el-table-column>
            <el-table-column
            label="操作"
            fixed="right">
            <template slot-scope="scope">
                <el-button type="success" size="small" @click="openUpdateUserProtalEntityByAuditId(scope.row)">审批</el-button>
                <el-button type="danger" size="small" @click="deleteUserProtalById(scope.row.id)" v-if="false">删除</el-button>
            </template>
            </el-table-column>
        </el-table>
    </div>
    <!-- 分页 -->
    <div style="margin-top: 20px;">
        <template>
            <div class="block">
                <el-pagination
                    background
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="currentPage"
                    :page-sizes="[5, 10, 15, 20]"
                    :page-size="5"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
                </el-pagination>
            </div>
        </template>
    </div>
    <!-- 审批弹窗 -->
    <el-dialog
        title="审批骑手"
        :visible.sync="UpdateUserProtalEntityDialogVisible"
        width="40%"
        >
            <el-button type="success" @click="updateHorsemanauDitIdSucceed()">通过</el-button>
            <el-button type="danger" @click="openRejectUserProtalEntityDialogVisible()">驳回</el-button>
    </el-dialog>
    <!-- 驳回弹窗 -->
    <el-dialog
        title="驳回理由"
        :visible.sync="rejectUserProtalEntityDialogVisible"
        width="40%"
        >
        <el-input
          type="textarea"
          :autosize="{ minRows: 4, maxRows: 4}"
          placeholder="请输入内容"
          v-model="reason">
        </el-input>
        <el-button type="success" style="margin-top: 20px" @click="sendEmail()">发送邮箱</el-button>
    </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 驳回理由
      reason: '',
      // 驳回理由弹窗
      rejectUserProtalEntityDialogVisible: false,
      // 审批数据集
      tableDataForm: [],
      // 审批弹窗
      UpdateUserProtalEntityDialogVisible: false,
      // 模糊查询（用户名称）
      userName: '',
      // 全查后台用户表数据集
      tableData: [],
      // 分页 （pageSize）一页展示5条数据 （pageNum）当前页
      pageRequest: {
        pageSize: 5,
        pageNum: 1
      },
      // 分页 前往第1页
      currentPage: 1,
      // 分页
      total: 0
    }
  },
  mounted () {
    // 全查后台用户表（模糊查用户名字）
    this.selectUserProtalEntity()
  },
  methods: {
    // 驳回消息发送邮箱
    sendEmail () {
      debugger
      this.tableDataForm = {
        id: this.tableDataForm.id,
        mail: this.tableDataForm.mail,
        reason: this.reason
      }
      this.$axios.put('/eat-service/userProtal/updateHorsemanauDitIdFail', this.tableDataForm).then(res => {
        if (res.data.flag) {
          this.rejectUserProtalEntityDialogVisible = false // 关闭窗口
          this.myMessage('success', '骑手驳回成功')
          this.selectUserProtalEntity()
        } else {
          this.myMessage('warning', '骑手驳回失败')
        }
      }).catch(res => {
        this.myMessage('erroe', '骑手驳回失败，请联系管理员')
      })
    },
    // 打开驳回消息弹窗
    openRejectUserProtalEntityDialogVisible () {
      debugger
      if (this.tableDataForm.mail === '') {
        this.updateHorsemanauDitIdFail()
      } else {
        this.UpdateUserProtalEntityDialogVisible = false // 关闭审批窗口
        this.rejectUserProtalEntityDialogVisible = true // 打开驳回理由窗口
      }
    },
    // 删除用户
    deleteUserProtalById (data) {
      this.$confirm('此操作将删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios.delete('/eat-service/userProtal/deleteUserProtalById',
          {
            params: {
              id: data
            }
          }).then(res => {
          if (res.status === 200) {
            this.myMessage('success', '删除成功')
            this.selectUserProtalEntity()
          } else {
            this.myMessage('warning', '删除失败')
          }
        }).catch(res => {
          this.myMessage('erroe', '删除失败，请联系管理员')
        })
      }).catch(() => {
        this.myMessage('info', '已取消删除')
      })
    },
    // 审批骑手驳回
    updateHorsemanauDitIdFail () {
      this.tableDataForm = {
        id: this.tableDataForm.id,
        mail: this.tableDataForm.mail,
        reason: this.reason
      }
      this.$axios.put('/eat-service/userProtal/updateHorsemanauDitIdFail', this.tableDataForm).then(res => {
        if (res.data.flag) {
          this.UpdateUserProtalEntityDialogVisible = false // 关闭窗口
          this.myMessage('success', '骑手驳回成功')
          this.selectUserProtalEntity()
        } else {
          this.myMessage('warning', '骑手驳回失败')
        }
      }).catch(res => {
        this.myMessage('erroe', '骑手驳回失败，请联系管理员')
      })
    },
    // 审批骑手通过
    updateHorsemanauDitIdSucceed () {
      const formData = new FormData()
      formData.set('id', this.tableDataForm.id)
      formData.set('mail', this.tableDataForm.mail)
      this.$axios.put('/eat-service/userProtal/updateHorsemanauDitIdSucceed', formData).then(res => {
        if (res.data.flag) {
          this.UpdateUserProtalEntityDialogVisible = false // 关闭窗口
          this.myMessage('success', '骑手通过成功')
          this.selectUserProtalEntity()
        } else {
          this.myMessage('warning', '骑手通过失败')
        }
      }).catch(res => {
        this.myMessage('erroe', '骑手通过失败，请联系管理员')
      })
    },
    // 打开审批骑手弹窗
    openUpdateUserProtalEntityByAuditId (data) {
      this.UpdateUserProtalEntityDialogVisible = true
      this.tableDataForm = {
        id: data.id,
        mail: data.mail
      }
    },
    // 全查后台用户表（模糊查用户名字）
    selectUserProtalEntity () {
      this.$axios.get('/eat-service/userProtal/getUserProtalByAudit',
        {
          params: {
            userName: this.userName,
            pageSize: this.pageRequest.pageSize,
            pageNum: this.pageRequest.pageNum
          }
        }
      ).then(res => {
        this.tableData = res.data.content
        this.total = res.data.total
      }).catch(error => {
        console.log(error)
      })
    },
    // 分页 每页条数
    handleSizeChange (val) {
      this.pageRequest.pageSize = val
      this.selectUserProtalEntity()
    },
    // 分页 当前页
    handleCurrentChange (val) {
      this.pageRequest.pageNum = val
      this.selectUserProtalEntity()
    },
    // 弹窗提示
    myMessage (type, message) {
      this.$message({
        type: type,
        message: message
      })
    }
  }
}
</script>

<style>
    .abvoeBox{
        width: 100%;
        height: 60px;
        text-align: left;
        margin-top: 5px;
    }
    .abvoeBox_input{
        width: 200px;
        height: 50px;
        margin-left: 10px;
    }
    .belowBox{
        margin-top: 20px;
    }
    .p1{
      border: none;
      background: none;
    }
    .p1:hover{
      color: #31c27c;
    }
    .span1{
      margin-left: 5px;
      color: red;
    }
</style>
